﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aandelendelen.Db;
using NLog;

namespace Aandelendelen
{
	public partial class BannerClicked : System.Web.UI.Page
	{
		private static Logger logger = LogManager.GetCurrentClassLogger();

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				string strId = this.Request.QueryString["id"];
				if (strId == null)
					return;

				string url = this.Request.QueryString["url"];
				if (url == null)
					return;

				try
				{
					this.RegisterClick(Convert.ToInt32(strId), url);
				}
				catch
				{
					return;
				}
			}
		}

		private void RegisterClick(int adId, string targetUrl)
		{
			logger.Info("Banner clicked. Id: {0}", adId.ToString());

			Query query = new Query("sp_updateclicks", "id", adId, System.Data.OleDb.OleDbType.Integer);
			query.Execute();

			Response.Redirect(targetUrl, true);
		}
	}
}